<?php

namespace frontend\modules\orders\models;

use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "invoice".
 *
 * @property string $invoice_id
 * @property string $order_id
 * @property integer $invoice_type
 * @property integer $is_payed
 * @property integer $porc
 * @property string $p_tel
 * @property string $c_name
 * @property string $c_code
 * @property string $p_email
 * @property string $order_sn
 *
 * @property OrderInfo $order
 */
class Invoice extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'invoice';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['invoice_id','default','value'=>$this->getUUID()],
            [['invoice_id', 'order_id'], 'required'],
            [['invoice_type', 'is_payed', 'porc'], 'integer'],
            [['invoice_id', 'order_id'], 'string', 'max' => 40],
            [['p_tel','order_sn'], 'string', 'max' => 20],
            [['c_name'], 'string', 'max' => 200],
            [['c_code'], 'string', 'max' => 25],
            [['p_email'], 'string', 'max' => 60],
            [['order_id'], 'exist', 'skipOnError' => true, 'targetClass' => OrderInfo::className(), 'targetAttribute' => ['order_id' => 'order_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'invoice_id' => 'Invoice ID',
            'order_id' => 'Order ID',
            'invoice_type' => 'Invoice Type',
            'is_payed' => 'Is Payed',
            'porc' => 'Porc',
            'p_tel' => 'P Tel',
            'c_name' => 'C Name',
            'c_code' => 'C Code',
            'p_email' => 'P Email',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOrder()
    {
        return $this->hasOne(OrderInfo::className(), ['order_id' => 'order_id']);
    }

    /**
     * @inheritdoc
     * @return \frontend\modules\orders\models\query\InvoiceQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\modules\orders\models\query\InvoiceQuery(get_called_class());
    }

    /**
     * @param $postData
     * @param $order_id
     * @param string data[invoice_type] |发票类型|yes|0或1或2 0：普通发票 1：电子发票 2：增值发票
     * @param string data[porc] |发票种类|yes|0或1 0：个人，1：公司
     * @param string data[c_name] |发票抬头公司全名|yes|
     * @param string data[c_code] |公司税号|yes|
     * @param string data[p_tel] |电话|yes|
     * @param string data[p_email] |邮箱|yes|
     * @throws Exception
     */
    public static function CreateInv($postData,$order_id)
    {
        $model = new Invoice();
        $model->invoice_type = isset($postData['invoice_type'])?$postData['invoice_type']:0;
        $model->porc =isset($postData['porc'])?$postData['porc']:0;
        $model->c_name = isset($postData['c_name'])?$postData['c_name']:'';
        $model->c_code = isset($postData['c_code'])?$postData['c_code']:'';;
        $model->p_tel = isset($postData['p_tel'])?$postData['p_tel']:'';;
        $model->p_email =  isset($postData['p_email'])?$postData['p_email']:'';;
        $model->order_id =  $order_id;
        $model->order_sn = OrderInfo::findOne($order_id)->order_sn;
        if(!$model->save()){
            throw new Exception(json_encode($model->errors));
        }
    }
}
